package com.wander.common;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * Created by zhaowentong on 2017/6/2.
 */
public class Page implements Serializable {
    private static final long serialVersionUID = 1L;
    private static int DEFAULT_PAGE_SIZE = 10;
    private int pageSize;
    private int totalCount;
    private int start;
    private int nowPage;
    private List<?> result;

    public Page() {
        this.pageSize = DEFAULT_PAGE_SIZE;
        this.result = Collections.emptyList();
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        if(pageSize != 0) {
            this.pageSize = pageSize;
        }

    }

    public int getTotalPage() {
        return (int)Math.ceil((double)this.totalCount * 1.0D / (double)this.pageSize);
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getStart() {
        this.start = (this.getNowPage() - 1) * this.getPageSize();
        if(this.start < 0) {
            this.start = 0;
        }

        return this.start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getNowPage() {
        if(this.nowPage <= 0) {
            this.nowPage = 1;
        }

        if(this.nowPage > this.getTotalPage()) {
            this.nowPage = this.getTotalPage();
        }

        return this.nowPage;
    }

    public void setNowPage(int nowPage) {
        this.nowPage = nowPage;
    }

    public List<?> getResult() {
        return this.result;
    }

    public void setResult(List<?> result) {
        this.result = result;
    }
}
